package mage.factory.handler;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class SetHandler extends DefaultHandler {
	private static Logger log = LogManager.getLogger(SetHandler.class);
	protected String SETLIST = "setlist";
	protected String SET = "set";
	
	protected String NAME = "name";
	
	protected String BLOCK = "block";
	protected String UNCOMMON = "uncommon";
	protected String RELEASE_DATE = "release-date";
	protected String CARDS = "cards";
	protected String COMMON = "common";
	protected String CODE = "code";
	protected String MYTHIC_RARE = "mythic-rare";
	protected String RARE = "rare";
	protected String BASIC_LAND = "basic-land";
	
	protected String set_package = "mage.sets";
	
	protected SetElement current_element = null;
	
	public boolean isRootElement(String name){
		return this.SET.equalsIgnoreCase(name);
	}
	public boolean isSetValid(){
		return this.current_element_valid && this.current_element!=null;
	}
	

	@Override
	public void startElement(String qName, Attributes attributes)throws SAXException {
		
		if(this.SET.equalsIgnoreCase(qName)){this.current_element=new SetElement();}
	}
	@Override
	public void endElement(String qName,String characters) throws SAXException {
		if(this.SET.equalsIgnoreCase(qName) ){
			if(this.isSetValid()){
				this.elements.add(this.current_element);
			}
		}		
	}
	@Override
	public boolean characters(String characters) throws SAXException {
		if(this.element_is_start){
			if(this.NAME.equalsIgnoreCase(super.getCurrentElement())){
				if(!this.isElementAlreadyExisting(characters,this.set_package)){
					this.current_element.setName(characters);
				}else{
					this.current_element=null;
					this.current_element_valid=false;
				}
				return true;
			}
			if(this.BLOCK.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setBlock(characters.trim());return true;}
			if(this.UNCOMMON.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setUncommon(characters.trim());return true;}
			if(this.RELEASE_DATE.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setRelease_date(characters.trim());return true;}
			if(this.CARDS.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setCards(characters.trim());return true;}
			if(this.COMMON.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setCommon(characters.trim());return true;}
			if(this.CODE.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setCode(characters.trim());return true;}
			if(this.MYTHIC_RARE.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setMythic_rare(characters.trim());return true;}
			if(this.RARE.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setRare(characters.trim());return true;}
			if(this.BASIC_LAND.equalsIgnoreCase(super.getCurrentElement()) && this.isSetValid()){this.current_element.setBasic_land(characters.trim());return true;}
			
		}
		return false;
	}


}
class SetElement implements DefaultHandlerElement{
	protected String name = "";
	protected String block = "";
	protected String uncommon ="0";
	protected String release_date ="0-0-0";
	protected String cards = "0";
	protected String common = "0";
	protected String code = "";
	protected String mythic_rare = "0";
	protected String rare = "0";
	protected String basic_land = "0";
	
	public SetElement(){
	}
	public String getBlock() {
		return block;
	}
	public void setBlock(String block) {
		this.block = block;
	}
	public String getUncommon() {
		return uncommon;
	}
	public void setUncommon(String uncommon) {
		this.uncommon = uncommon;
	}
	public String getRelease_date() {
		return release_date;
	}
	public void setRelease_date(String release_date) {
		this.release_date = release_date;
	}
	public String getCards() {
		return cards;
	}
	public void setCards(String cards) {
		this.cards = cards;
	}
	public String getCommon() {
		return common;
	}
	public void setCommon(String common) {
		this.common = common;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getMythic_rare() {
		return mythic_rare;
	}
	public void setMythic_rare(String mythic_rare) {
		this.mythic_rare = mythic_rare;
	}
	public String getRare() {
		return rare;
	}
	public void setRare(String rare) {
		this.rare = rare;
	}
	public String getName() {
		if(this.name.contains(":")){
			this.name=this.name.split(":")[1];
		}
		this.name=this.name.replace(" vs. ","VS");
		return name;
	}
	public String getClassName(){
		return this.getName().replace(" ","").replaceAll("'","").replaceAll("\"","");
	}
	public String getSetName(){
		return "mage.sets."+this.getPackageName();
	}
	public String getPackageName(){
		return this.getClassName().toLowerCase();
	}
	public String getRelease_year(){
		try{
			return this.getRelease_date().split("-")[0];
		}catch(ArrayIndexOutOfBoundsException e){
			return "0";
		}
	}
	public String getRelease_month(){
		try{
			return String.valueOf(Integer.parseInt(this.getRelease_date().split("-")[1]));
		}catch(ArrayIndexOutOfBoundsException e){
			return "0";
		}	
	}
	public String getRelease_day(){
		try{
			return String.valueOf(Integer.parseInt(this.getRelease_date().split("-")[2]));
		}catch(ArrayIndexOutOfBoundsException e){
			return "1";
		}
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType(){
		if(this.getName().equalsIgnoreCase(this.getBlock())){
			return "Constants.SetType.CORE";
		}else{
			return "Constants.SetType.EXPANSION";
		}
	}
	public String toClass(){
		StringBuffer sb = new StringBuffer();
		/*
		sb.append("/*\n");
		sb.append("* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.\n");
		sb.append("*\n");
		sb.append("* Redistribution and use in source and binary forms, with or without modification, are\n");
		sb.append("* permitted provided that the following conditions are met:\n");
		sb.append("*\n");
		sb.append("*    1. Redistributions of source code must retain the above copyright notice, this list of\n");
		sb.append("*       conditions and the following disclaimer.\n");
		sb.append("*\n");
		sb.append("*    2. Redistributions in binary form must reproduce the above copyright notice, this list\n");
		sb.append("*       of conditions and the following disclaimer in the documentation and/or other materials\n");
		sb.append("*       provided with the distribution.\n");
		sb.append("*\n");
		sb.append("* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED\n");
		sb.append("* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND\n");
		sb.append("* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR\n");
		sb.append("* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\n");
		sb.append("* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n");
		sb.append("* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON\n");
		sb.append("* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\n");
		sb.append("* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF\n");
		sb.append("* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n");
		sb.append("*\n");
		sb.append("* The views and conclusions contained in the software and documentation are those of the\n");
		sb.append("* authors and should not be interpreted as representing official policies, either expressed\n");
		sb.append("* or implied, of BetaSteward_at_googlemail.com.\n");
		*/
		//sb.append("*/\n");
		//sb.append("\n");
		sb.append("package mage.sets;\n");
		sb.append("import mage.Constants;\n");
		sb.append("import mage.cards.ExpansionSet;\n");
		sb.append("import java.util.GregorianCalendar;\n");
		//sb.append("/**\n");
		//sb.append(" *\n");
		//sb.append(" * @author BetaSteward_at_googlemail.com\n");
		//sb.append(" */\n");
		sb.append("public class "+this.getClassName()+" extends ExpansionSet {\n");
		sb.append("\tprivate static final long serialVersionUID = 1L;\n");
		sb.append("\tprivate static final "+this.getClassName()+" fINSTANCE =  new "+this.getClassName()+"();\n");
		sb.append("\tpublic static "+this.getClassName()+" getInstance() {\n");
		sb.append("\t\treturn fINSTANCE;\n");
		sb.append("\t}\n");
		sb.append("\tprivate "+this.getClassName()+"() {\n");
		sb.append("\t\tsuper(\""+this.getName().replaceAll("\"","")+"\", \""+this.getCode()+"\", \"seticon_"+this.getCode()+"\", \""+this.getSetName()+"\", new GregorianCalendar("+this.getRelease_year()+", "+this.getRelease_month()+","+this.getRelease_day()+").getTime(), "+this.getType()+");\n");
		sb.append("\t\tthis.blockName = \""+this.getBlock()+"\";\n");
		//this.parentSet = ShardsOfAlara.getInstance();
		sb.append("\t\tthis.hasBoosters = true;\n");
		sb.append("\t\tthis.numBoosterLands = "+this.getBasic_land()+";\n");
		sb.append("\t\tthis.numBoosterCommon = "+this.getCommon()+";\n");
		sb.append("\t\tthis.numBoosterUncommon = "+this.getUncommon()+";\n");
		sb.append("\t\tthis.numBoosterRare = "+this.getRare()+";\n");
		sb.append("\t\tthis.ratioBoosterMythic = "+this.getMythic_rare()+";\n");
		sb.append("\t}\n\n}\n");
		return sb.toString();
	}
	public String getBasic_land() {
		return basic_land;
	}
	public void setBasic_land(String basic_land) {
		this.basic_land = basic_land;
	}
}